﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

namespace Terraria.ModLoader
{
	// Token: 0x02000185 RID: 389
	public static class CommandManager
	{
		// Token: 0x06000F74 RID: 3956 RVA: 0x0000E79A File Offset: 0x0000C99A
		public static bool Matches(CommandType commandType, CommandType callerType)
		{
			if ((commandType & CommandType.World) != (CommandType)0)
			{
				if (Main.netMode == 2)
				{
					commandType |= CommandType.Server;
				}
				else if (Main.netMode == 0)
				{
					commandType |= CommandType.Chat;
				}
			}
			return (callerType & commandType) > (CommandType)0;
		}

		// Token: 0x06000F75 RID: 3957 RVA: 0x00403544 File Offset: 0x00401744
		internal static void Add(ModCommand cmd)
		{
			List<ModCommand> list;
			if (!CommandManager.Commands.TryGetValue(cmd.Command, out list))
			{
				CommandManager.Commands.Add(cmd.Command, list = new List<ModCommand>());
			}
			list.Add(cmd);
		}

		// Token: 0x06000F76 RID: 3958 RVA: 0x0000E7C2 File Offset: 0x0000C9C2
		internal static void Unload()
		{
			CommandManager.Commands.Clear();
		}

		// Token: 0x06000F77 RID: 3959 RVA: 0x00403584 File Offset: 0x00401784
		internal static bool GetCommand(CommandCaller caller, string name, out ModCommand mc)
		{
			string text = null;
			if (name.Contains(':'))
			{
				string[] array = name.Split(new char[]
				{
					':'
				});
				text = array[0];
				name = array[1];
			}
			mc = null;
			List<ModCommand> list;
			if (!CommandManager.Commands.TryGetValue(name, out list))
			{
				return false;
			}
			list = (from c in list
			where CommandManager.Matches(c.Type, caller.CommandType)
			select c).ToList<ModCommand>();
			if (list.Count == 0)
			{
				return false;
			}
			if (text != null)
			{
				Mod mod = ModLoader.GetMod(text);
				if (mod == null)
				{
					caller.Reply("Unknown Mod: " + text, Color.Red);
				}
				else
				{
					mc = list.SingleOrDefault((ModCommand c) => c.mod == mod);
					if (mc == null)
					{
						caller.Reply(string.Concat(new string[]
						{
							"Mod: ",
							text,
							" does not have a ",
							name,
							" command."
						}), Color.Red);
					}
				}
			}
			else
			{
				if (list.Count > 1)
				{
					caller.Reply("Multiple definitions of command /" + name + ". Try:", Color.Red);
					using (List<ModCommand>.Enumerator enumerator = list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							ModCommand modCommand = enumerator.Current;
							caller.Reply(modCommand.mod.Name + ":" + modCommand.Command, Color.LawnGreen);
						}
						return true;
					}
				}
				mc = list[0];
			}
			return true;
		}

		// Token: 0x06000F78 RID: 3960 RVA: 0x00403738 File Offset: 0x00401938
		internal static bool HandleCommand(string input, CommandCaller caller)
		{
			string[] array = input.TrimEnd(new char[0]).Split(new char[]
			{
				' '
			});
			string text = array[0];
			array = array.Skip(1).ToArray<string>();
			if (caller.CommandType != CommandType.Console)
			{
				if (text[0] != '/')
				{
					return false;
				}
				text = text.Substring(1);
			}
			ModCommand modCommand;
			if (!CommandManager.GetCommand(caller, text, out modCommand))
			{
				return false;
			}
			if (modCommand == null)
			{
				return true;
			}
			try
			{
				modCommand.Action(caller, input, array);
			}
			catch (Exception ex)
			{
				UsageException ex2 = ex as UsageException;
				if (ex2 != null && ex2.msg != null)
				{
					caller.Reply(ex2.msg, ex2.color);
				}
				else
				{
					caller.Reply("Usage: " + modCommand.Usage, Color.Red);
				}
			}
			return true;
		}

		// Token: 0x06000F79 RID: 3961 RVA: 0x0040380C File Offset: 0x00401A0C
		public static List<Tuple<string, string>> GetHelp(CommandType type)
		{
			List<Tuple<string, string>> list = new List<Tuple<string, string>>();
			Func<ModCommand, bool> <>9__0;
			foreach (KeyValuePair<string, List<ModCommand>> keyValuePair in CommandManager.Commands)
			{
				IEnumerable<ModCommand> value = keyValuePair.Value;
				Func<ModCommand, bool> predicate;
				if ((predicate = <>9__0) == null)
				{
					predicate = (<>9__0 = ((ModCommand mc) => CommandManager.Matches(mc.Type, type)));
				}
				List<ModCommand> list2 = value.Where(predicate).ToList<ModCommand>();
				foreach (ModCommand modCommand in list2)
				{
					string text = modCommand.Command;
					if (list2.Count > 1)
					{
						text = modCommand.mod.Name + ":" + text;
					}
					list.Add(Tuple.Create<string, string>(text, modCommand.Description));
				}
			}
			return list;
		}

		// Token: 0x040012E1 RID: 4833
		internal static readonly IDictionary<string, List<ModCommand>> Commands = new Dictionary<string, List<ModCommand>>(StringComparer.OrdinalIgnoreCase);
	}
}
